﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class PipeChecked : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			TransactionGroup transactionGroup = new TransactionGroup(commandData.Application.ActiveUIDocument.Document, "DetectOverlappingPipe");
			try
			{
				transactionGroup.Start();
				FormPipeCheckedCResult formPipeCheckedCResult = new FormPipeCheckedCResult(commandData);
				if (formPipeCheckedCResult.DialogResult == DialogResult.Retry)
				{
					while (formPipeCheckedCResult.DialogResult == DialogResult.Retry)
					{
						PipeFilter pipeFilter = new PipeFilter();
						IList<Element> list = commandData.Application.ActiveUIDocument.Selection.PickElementsByRectangle(pipeFilter, "请框选要检查的水管：");
						if (list.Count < 2)
						{
							YJKMessageBox.Information("请选择多于两条管线！");
						}
						else
						{
							formPipeCheckedCResult = new FormPipeCheckedCResult(commandData, list);
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
			catch (Exception ex)
			{
				transactionGroup.RollBack();
				message += ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				transactionGroup.Commit();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
